package com.zxy.furns.service.impl;

import com.zxy.furns.dao.FurnDAO;
import com.zxy.furns.dao.impl.FurnDAOImpl;
import com.zxy.furns.entity.Furn;
import com.zxy.furns.entity.Page;
import com.zxy.furns.service.FurnService;

import java.util.List;

/**
 * ClassName: FurnServiceImpl
 * Package: com.zxy.furns.service.impl
 *
 * @Author zxy
 * @Create 2025/3/21 15:00
 * @Version 1.0
 * title:
 * Description:
 */
public class FurnServiceImpl implements FurnService {
    private FurnDAO furnDAO = new FurnDAOImpl();

    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }

    @Override
    public int addFurn(Furn furn) {
        return furnDAO.addFurn(furn);
    }

    @Override
    public int delFurn(int id) {
        return furnDAO.deleteFurn(id);
    }

    @Override
    public Furn queryFurnById(int id) {
        return furnDAO.queryFurnById(id);
    }

    @Override
    public int updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn);
    }

    @Override
    public int getTotalCount() {
        return furnDAO.getTotalRow();
    }

    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        //先创建一个Page对象，再根据实际情况填充数据
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = furnDAO.getTotalRow();
        page.setTotalRow(totalRow);
        //pageTotalCount是计算得到的 -->算法问题

        int pageTotalCount = totalRow / pageSize;
        if (totalRow % pageSize > 0) {
            pageTotalCount += 1;
        }

        page.setPageTotalCount(pageTotalCount);

        //private List<T> items
        //开始计算begin->小算法
        int begin = (pageNo - 1) * pageSize;
        List<Furn> pageItems = furnDAO.getPageItems(begin, pageSize);
        page.setItems(pageItems);

        //还差一个url 后面再讲解
        return page;

    }

    @Override
    public Page<Furn> pageByName(int pageNo, int pageSize, String name) {
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int pageTotalRow = furnDAO.getPageTotalRowByName(name);
        page.setTotalRow(pageTotalRow);
        int pageTotalCount = pageTotalRow / pageSize;
        if (pageTotalRow % pageSize > 0) {
            pageTotalCount += 1;
        }
        page.setPageTotalCount(pageTotalCount);
        int begin = (pageNo - 1) * pageSize;

        List<Furn> items = furnDAO.getPageItemByName(begin, pageSize, name);
        page.setItems(items);



        return page;

    }


}
